package com.new_wear.project.system.task.controller;

import java.util.List;

import com.new_wear.common.utils.security.ShiroUtils;
import com.new_wear.project.system.dept.domain.Dept;
import com.new_wear.project.system.dept.service.IDeptService;
import com.new_wear.project.system.pos.domain.CstAdPos;
import com.new_wear.project.system.taskdo.domain.CstTaskDo;
import com.new_wear.project.system.taskdo.service.ICstTaskDoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.new_wear.framework.aspectj.lang.annotation.Log;
import com.new_wear.framework.aspectj.lang.enums.BusinessType;
import com.new_wear.project.system.task.domain.CstTask;
import com.new_wear.project.system.task.service.ICstTaskService;
import com.new_wear.framework.web.controller.BaseController;
import com.new_wear.framework.web.domain.AjaxResult;
import com.new_wear.common.utils.poi.ExcelUtil;
import com.new_wear.framework.web.page.TableDataInfo;

/**
 * 系统 - 任务主体Controller
 * 
 * @author new_wear
 * @date 2020-01-09
 */
@Controller
@RequestMapping("/system/task")
public class CstTaskController extends BaseController
{
    private String prefix = "system/task";

    @Autowired
    private ICstTaskService cstTaskService;

    @Autowired
    private IDeptService deptService;

    @Autowired
    private ICstTaskDoService cstTaskDoService;

    @RequiresPermissions("system:task:view")
    @GetMapping()
    public String task()
    {
        return prefix + "/task";
    }

    /**
     * 查询系统 - 任务主体列表
     */
    @RequiresPermissions("system:task:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CstTask cstTask)
    {
        startPage();
        cstTask.setIsPlan(1);
        List<CstTask> list = cstTaskService.selectCstTaskList(cstTask);
        return getDataTable(list);
    }

    /**
     * 导出系统 - 任务主体列表
     */
    @RequiresPermissions("system:task:export")
    @Log(title = "系统 - 任务主体", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CstTask cstTask)
    {
        List<CstTask> list = cstTaskService.selectCstTaskList(cstTask);
        ExcelUtil<CstTask> util = new ExcelUtil<CstTask>(CstTask.class);
        return util.exportExcel(list, "task");
    }

    /**
     * 新增系统 - 任务主体
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存系统 - 任务主体
     */
    @RequiresPermissions("system:task:add")
    @Log(title = "系统 - 任务主体", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CstTask cstTask)
    {
        cstTask.setIsPlan(1);
        Long deptId = ShiroUtils.getSysUser().getDeptId();
        cstTask.setAreaId(deptId.intValue());
        return toAjax(cstTaskService.insertCstTask(cstTask));
    }

    /**
     * 修改系统 - 任务主体
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        CstTask cstTask = cstTaskService.selectCstTaskById(id);
        mmap.put("cstTask", cstTask);
        return prefix + "/edit";
    }

    /**
     * 修改保存系统 - 任务主体
     */
    @RequiresPermissions("system:task:edit")
    @Log(title = "系统 - 任务主体", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CstTask cstTask)
    {
        return toAjax(cstTaskService.updateCstTask(cstTask));
    }

    /**
     * 删除系统 - 任务主体
     */
    @RequiresPermissions("system:task:remove")
    @Log(title = "系统 - 任务主体", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(cstTaskService.deleteCstTaskByIds(ids));
    }


    /*指派*/
    @GetMapping("/assign/{id}")
    public String assign(@PathVariable("id") Integer id,ModelMap map)
    {
        map.put("cstTask",cstTaskService.selectCstTaskById(id));
        return prefix + "/assign";
    }

    /*指派地区人员页面跳转*/
    @GetMapping("/appointees")
    public String appointees()
    {
        return prefix + "/appointees";
    }
    /*指派地区列表擦查询*/
    @RequestMapping("/intees")
    @ResponseBody
    public TableDataInfo adpos(){
        startPage();
        List<Dept> deptList = deptService.selectDeptList(new Dept());//指派地区选择列表

        return getDataTable(deptList);
    }

    /*选择*/
    @RequestMapping("/choice")
    @ResponseBody
    public Dept choice( Long deptId){
        Dept dept = deptService.selectDeptById(deptId);
        startPage();
        return dept;
    }

    /*指派任务*/
    @RequestMapping("/taskDo")
    @ResponseBody
    public AjaxResult taskDo(@RequestParam("dId") Integer dId , CstTask cstTask){

        CstTaskDo cstTaskDo = new CstTaskDo();//指派列表对象
        cstTaskDo.setAreaId(dId);//地区id
        cstTaskDo.setTid(cstTask.getId());//任务id，任务表
        cstTaskDo.setFinishTime(cstTask.getFinishTime());//完成时间
        cstTaskDo.setStatus(0);
        return toAjax( cstTaskDoService.insertCstTaskDo(cstTaskDo));
    }
}
